<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>待办事项</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    .container {
        width: 800px;
        margin: 0 auto;
        display: flex;
    }

    .todo,
    .done {
        width: 50%;
        height: 100%;
    }

    .container h3 {
        height: 50px;
        text-align: center;
        line-height: 50px;
        background-color: #333;
        color: #fff;
    }

    .nav {
        width: 800px;
        height: 100px;
        margin: 0 auto;
        display: flex;
        align-items: center;
    }

    .nav input {
        width: 600px;
        height: 50px;
    }

    .nav button {
        width: 200px;
        height: 50px;
        border: none;
        background-color: orange;
        color: #fff;
    }

    .row {
        height: 50px;
        display: flex;
        align-items: center;
    }

    .row input {
        margin: 0 10px;
    }

    .row span {
        width: 300px;
    }

    .row button {
        width: 50px;
        height: 40px;
    }
</style>

<body>
    <div class="nav">
        <input type="text">
        <button>新建任务</button>
    </div>
    <div class="container">
        <div class="todo">
            <h3>未完成</h3>
            <div class="row">
                <input type="checkbox">
                <span>吃饭</span>
                <button>删除</button>
            </div>
        </div>
        <div class="done">
            <h3>已完成</h3>
        </div>
    </div>
</body>
<script>
    //实现新增任务
    let addTaskButton = document.querySelector('.nav button');
    addTaskButton.onclick = () => {
        //获取到任务内容的输入框
        let input = document.querySelector('.nav input');
        //获取到输入框内容
        let taskContent = input.value;
        //根据内容新建一个元素节点
        let row = document.createElement('div');
        row.className = 'row';
        let checkbox = document.createElement('input');
        checkbox.type = 'checkbox';
        let span = document.createElement('span');
        span.innerHTML = taskContent;
        let button = document.createElement('button');
        button.innerHTML = '删除';
        row.appendChild(checkbox);
        row.appendChild(span);
        row.appendChild(button);
        //把新节点插入到todo中;
        let todo = document.querySelector('.todo');
        todo.appendChild(row);
        input.value = '';
        checkbox.onclick = () => {
            // 
            let row = checkbox.parentNode;
            // 注意! 是先触发 checked 为 true, 然后再调用 onclick 函数
            if (checkbox.checked) {
                let target = document.querySelector('.done');
                target.appendChild(row);
            } else {
                let target = document.querySelector('.todo');
                target.appendChild(row);
            }

        }
        button.onclick = () => {
            let row = button.parentNode;
            let rowparent = row.parentNode;
            rowparent.removeChild(row)
        }

    }
</script>

</html>